﻿using System;
using System.Composition;
using System.Threading.Tasks;
using Unread.Business.Repositories.Interfaces;
using Unread.Business.Services.Interfaces;

namespace Unread.Business.Services
{
    [Export(typeof(IFeedReadMarker))]
    internal class FeedReadMarker : IFeedReadMarker
    {
        private readonly IFeedRepository _feedRepository;

        [ImportingConstructor]
        public FeedReadMarker(IFeedRepository feedRepository)
        {
            if (feedRepository == null) throw new ArgumentNullException("feedRepository");

            _feedRepository = feedRepository;
        }

        public async Task MarkAsRead(Guid feedId)
        {
            var feed = await _feedRepository.GetById(feedId);
            if (feed == null)
                throw new InvalidOperationException(string.Format("Feed not found by id '{0}'", feedId));

            feed.LastRead = DateTime.Now;

            await _feedRepository.Update(feed);
        }
    }
}